package com.fengwk.support.websocket;

import org.springframework.context.annotation.Bean;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

/**
 * WebSocket配置,集成该类并注册为bean后必须实现WebSocketSessionManager
 */
public abstract class AbstractWebSocketConfig implements WebSocketConfigurer  {

	private final String WEB_SOCKET_URI = "/websocket/server";
	private final String SOCKET_JS_URI = "/sockjs/server";
	
	@Override
	public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
		registry.addHandler(webSocketHandler(), WEB_SOCKET_URI).addInterceptors(handshakeInterceptor());
        registry.addHandler(webSocketHandler(), SOCKET_JS_URI).addInterceptors(handshakeInterceptor()).withSockJS();
	}

    @Bean
    public TextWebSocketHandler webSocketHandler(){
        return new WebSocketHandler();
    }
    
    @Bean
    public HttpSessionHandshakeInterceptor handshakeInterceptor() {
    	return new HandshakeInterceptor();
    }
	
}